package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.OrderCommentBo;
import com.cskaoyan.market.db.domain.MarketOrderGoods;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxOrderService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: 鲸落
 * @Date: 2024/05/20/16:17
 * @Version: 1.0
 * @Description:
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    SecurityManager securityManager;
    @Autowired
    WxOrderService wxOrderService;

    @GetMapping("list") // 功能正常
    public Object list(Integer showType, Integer page, Integer limit) {
        // 从shiro中取出当前登录用户
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        // 调用service层方法
        return wxOrderService.list(showType, page, limit, user);
    }

    @GetMapping("detail") // 功能正常
    public Object detail(Integer orderId) {
        // 调用service层方法
        Map<String, Object> data = wxOrderService.detail(orderId);
        return ResponseUtil.ok(data);
    }

    @PostMapping("delete")  // 逻辑删除，功能正常
    public Object delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.delete(orderId);
        return ResponseUtil.ok();
    }

    @PostMapping("refund")  // 功能正常
    public Object refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        // 调用service层方法
        wxOrderService.refund(orderId);
        return ResponseUtil.ok();
    }

    @PostMapping("confirm") // 功能正常
    public Object confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.confirm(orderId);
        return ResponseUtil.ok();
    }

    @GetMapping("goods")    // 功能正常
    public Object goods(Integer orderId, Integer goodsId) {
        MarketOrderGoods data = wxOrderService.goods(orderId, goodsId);
        return ResponseUtil.ok(data);
    }
    @PostMapping("cancel")  // 功能正常
    public Object cancel(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        wxOrderService.cancel(orderId);
        return ResponseUtil.ok();
    }
    @PostMapping("comment") // 功能正常
    public  Object comment(@RequestBody OrderCommentBo orderCommentBo){
        // 从shiro中取出当前登录用户
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        wxOrderService.comment(orderCommentBo,user.getId());
        return ResponseUtil.ok();
    }
}
